<template>
  <div class="location">
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <el-button type="primary" @click="LocationLists" :loading="loading">新增区域</el-button>
      <div class="header"><div class="title">库位列表</div><span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span></div>
      <!-- 表格 -->
      <lin-table
        :tableColumn="tableColumn"
        :tableData="tableData"
        :operate="operate"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @row-click="rowClick"
        v-loading="loading"
      ></lin-table>
    </div>
    <div v-else>
      <el-dialog title="编辑区域" :append-to-body="true" :before-close="handleClose" :visible.sync="showEdit">
        <div style="margin-top: -25px">
          <el-tabs>
            <wh-location-edit @editCloses="editCloses" :editLocationID="editLocationID"></wh-location-edit>
          </el-tabs>
        </div>
      </el-dialog>
    </div>
    <div v-if="showAdd">
      <el-dialog title="新增区域" :append-to-body="true" :before-close="handleClose" :visible.sync="showAdd">
        <div style="margin-top: -25px">
          <el-tabs>
            <wh-location-add @addCloses="addCloses" :editLocationID="editLocationID"></wh-location-add>
          </el-tabs>
        </div>
      </el-dialog>
    </div>
    <!-- 编辑页面 -->
    <!-- <book-modify v-else @editClose="editClose" :editBookID="editBookID"></book-modify> -->
    
  </div>
</template>

<script>
import warehouse from '@/model/warehouse'
import LinTable from '@/component/base/table/lin-table'
import WhLocationAdd from './wh-location-add.vue'
import WhLocationEdit from './wh-location-edit.vue'
// import BookModify from './book-modify'

export default {
  props: {
    editAreaID: {
      type: Number,
    },
    areaId: {
      type: Number,
    }
  },
  components: {
    LinTable,
    WhLocationAdd,
    WhLocationEdit,
    // BookModify,
  },
  data() {
    return {
      tableColumn: [
        { prop: 'name', label: '库位名称' },
        { prop: 'location_order', label: '库位优先级' },
      ],
      tableData: [],
      operate: [],
      showEdit: false,
      editLocationID: 1,
      showAdd: false
    }
  },
  async created() {
    this.loading = true
    await this.getLocation()
    this.operate = [
      { name: '编辑', func: 'handleEdit', type: 'primary' },
      {
        name: '删除',
        func: 'handleDelete',
        type: 'danger',
        permission: '删除库位',
      },
    ]
    this.loading = false
  },
  methods: {
    async getLocation() {
      this.editLocationID = this.areaId
      console.log(this.areaId);
      try {
        const location = await warehouse.LocationList(this.areaId)
        console.log(location);
        this.tableData = location.items
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = []
        }
      }
    },
    handleEdit(val) {
      console.log('val', val)
      this.showEdit = true
      this.editLocationID = val.row.id
    },
    LocationLists() {
      this.showAdd = true
    },
    addCloses() {
      this.showAdd = false
      this.getLocation()
    },
    editCloses() {
      this.showEdit = false
      this.getLocation()
    },
    handleClose(done) {
      this.showAdd = false
      console.log(done);
      done()
    },
    back() {
      this.$emit('listClose')
    },
    addArea() {

    },
    handleDelete(val) {
      this.$confirm('此操作将永久删除该库位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await warehouse.deleteLocation(val.row.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getLocation()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    rowClick() {},
    editClose() {
      this.showEdit = false
      this.getLocation()
    },
  },
}
</script>

<style lang="scss" scoped>
.location {
  width: 100%;
}
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
